﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArkadiaOsKernel.Core.Filesystem.Vfs
{
    class Disk
    {
        public List<File> files = new List<File>();
        //Drive name.
        public string driveletter = "";

        //Disk handlers.
        #region " New Disk Handlers "
        public Disk()
        {

        }
        public Disk(string letter)
        {
            this.driveletter = letter;
        }
        #endregion
        #region " Start the filesystem "
        public void Start(int size, string letter)
        {
            //Start the Virtual File Disk with a set size.
            this.files.Clear();
            this.driveletter = letter;
        }
        public void Start(uint size, string letter)
        {
            //Start the Virtual File Disk with a set size.
            this.files.Clear();
            this.driveletter = letter;
        }
        public void Start()
        {
            //Start the Virtual File Disk with a defualt size.
            this.files.Clear();
        }
        #endregion
        #region " New File Handlers "
        public void AddFile(File newfile)
        {
            this.files.Add(newfile);
        }
        public void AddFile(string name, string data, Directory path)
        {
            File newfile = new File();
            newfile.name = name;
            newfile.data = data;
            newfile.path = path;
            newfile.SetSize(File.SizeType.Bytes);
            files.Add(newfile);
        }
        public void AddFile(string name, string data)
        {
            File newfile = new File();
            newfile.name = name;
            newfile.data = data;
            newfile.SetSize(File.SizeType.Bytes);
            files.Add(newfile);
        }
        public void AddFile(string name, string data, string type)
        {
            File newfile = new File();
            newfile.name = name;
            newfile.data = data;
            newfile.filetype = type;
            newfile.SetSize(File.SizeType.Bytes);
            files.Add(newfile);
        }
        public void AddFile(string name, string data, string type,string description)
        {
            File newfile = new File();
            newfile.name = name;
            newfile.data = data;
            newfile.filetype = type;
            newfile.description = description;
            newfile.SetSize(File.SizeType.Bytes);
            files.Add(newfile);
        }
        #endregion
        #region " Remove File Handlers "
        public void RemoveFile(int i)
        {
            files.RemoveAt(i);
        }
        public void RemoveFile(string name)
        {
            for (int i = 0; i < files.Count; i++)
            {
                if (this.files[i].name == name)
                {
                    files.RemoveAt(i);
                    break;
                }
            }
        }
        public void RemoveFile(int start, int end)
        {
            for (int i = start; i < end; i++)
            {
                files.RemoveAt(i);
            }
        }
        #endregion
        #region " Disk Handlers "
        public void PartitionDisk()
        {
            this.files.Clear();
        }
        public void CleanUpFiles(int sizeamount)
        {
            for (int i = 0; i < files.Count; i++)
            {
                this.files[i].SetSize(File.SizeType.Bytes);
                if (this.files[i].size <= sizeamount)
                {
                    files.RemoveAt(i);
                }
            }
        }
        public int GetTotalSize()
        {
            int size = 0;
            for (int i = 0; i < files.Count; i++)
            {
                this.files[i].SetSize(File.SizeType.Bytes);
                size += this.files[i].size;
            }
            return size;
        }
        #endregion
        #region " File helpers "
        public int GetFiles()
        {
            return files.Count;
        }
        #endregion
        #region " Stream Handlers "
        //public Stream GetFileContents(string name)
        //{
        //    for (int i = 0; i < this.index; i++)
        //    {
        //        if (this.files[i].name == name)
        //        {
        //            return new Stream(this.files[i].data);
        //        }
        //    }
        //    return new Stream("");
        //}
        public string GetFileContents(string nme)
        {
            for (int i = 0; i < files.Count; i++)
            {
                if (this.files[i].name == nme)
                {
                    return this.files[i].data;
                }
            }
            return "";
        }
        #endregion
    }
}
